import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:top_foundation/utils/theme/theme.dart';
import 'package:whale/helper/utils/util.dart';

import 'audio.dart';

class RecordAddItemWidget extends StatelessWidget {
  const RecordAddItemWidget(
    this.title, {
    Key? key,
    this.editText,
    this.placeholder,
    this.onChanged,
    this.audios,
    this.audioOnChanged,
    this.bottom,
    this.maxCount,
  }) : super(key: key);
  final String title;
  final String? placeholder;
  final String? editText;
  final List<AudioListModel>? audios;
  final Widget? bottom;
  final Function(String text)? onChanged;
  final Function(List<AudioListModel>?)? audioOnChanged;
  final int? maxCount;

  @override
  Widget build(BuildContext context) {
    return Card(
      child: Container(
        padding: EdgeInsets.fromLTRB(10.r, 10.r, 0, 0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              title,
              style: ITheme.text.titleLarge,
            ),
            RecordInputField(
              editText: editText,
              placeholder: placeholder,
              onChanged: onChanged,
              audios: audios,
              audioOnChanged: audioOnChanged,
              maxCount: maxCount,
            ),
            bottom ??
                SizedBox(
                  height: 10.r,
                ),
          ],
        ),
      ),
    );
  }
}

class RecordInputField extends StatefulWidget {
  const RecordInputField({
    Key? key,
    this.editText,
    this.placeholder,
    this.onChanged,
    this.audios,
    this.audioOnChanged,
    this.maxCount,
  }) : super(key: key);
  final String? placeholder;
  final String? editText;
  final List<AudioListModel>? audios;
  final Function(String text)? onChanged;
  final Function(List<AudioListModel>?)? audioOnChanged;
  final int? maxCount;
  @override
  State<RecordInputField> createState() => _RecordInputFieldState();
}

class _RecordInputFieldState extends State<RecordInputField> {
  late List<AudioListModel> audios;
  late String editText;

  @override
  void initState() {
    editText = widget.editText ?? '';
    audios = widget.audios ?? [];
    super.initState();
  }

  @override
  void didUpdateWidget(RecordInputField oldWidget) {
    super.didUpdateWidget(oldWidget);
    editText = widget.editText ?? '';
    audios = widget.audios ?? [];
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Expanded(
          child: UITextField(
            height: 180.r,
            data: editText,
            maxLines: 5,
            maxLength: widget.maxCount,
            onChanged: (t) {
              editText = t;
              if (widget.onChanged != null) {
                widget.onChanged!(t);
              }
            },
            hintText: widget.placeholder ?? Ls.qshuru,
            padding: EdgeInsets.symmetric(horizontal: 10.r, vertical: 15.r),
          ),
        ),
        AudioView(
          result: (p0) {
            if (widget.audioOnChanged != null) {
              audios.add(p0);
              editText += p0.trans ?? '';
              setState(() {});
              widget.audioOnChanged!(audios);
              if (widget.onChanged != null) {
                widget.onChanged!(editText);
              }
            }
          },
        ),
      ],
    );
  }
}

class AudioListModel {
  String? id;
  String? sourcePath;
  String? duration;
  String? trans;
  bool? show;
  bool isPlaying = false;

  AudioListModel({
    this.id,
    this.duration,
    this.sourcePath,
    this.show,
    this.trans,
  });

  factory AudioListModel.fromJson(Map<String, dynamic> json) => AudioListModel(
        id: json['id'],
        sourcePath: json['sourcePath'],
        duration: json['duration'].toString(),
        show: json['show'] as bool?,
        trans: json['trans'],
      );

  Map<String, dynamic> toJson() => {
        'id': id,
        'sourcePath': sourcePath,
        'duration': duration,
        'show': show,
        'trans': trans,
      };
}
